<template>
<!-- 给盒子注册双击事件 -->
  <div class="my-tag" @dblclick='open'>
    <input
   
    v-if='edit' 
    
    v-focus
    @blur='close'
     
    @keyup.enter='close'
      class="input"
      type="text"
      placeholder="输入标签"
      
      v-model='tag'
    />
    <div v-else class="text">{{value}}</div>
  </div>
</template>

<script>
export default {
  // 子组件MyTag.vue接收渲染
  props: ['value'],
  directives: {
focus:{
  inserten(el){
    el.focus();
  }
}
  },
data(){
  // 子组件MyTag.vue提供数据，并绑定
  return {
    edit:false,//表示编辑状态
    tag:''
  }
},
methods:{
  // 双击显示时回显
  open(){
    this.edit = true;
    this.tag = this.value
  },
  // 回车时、失去焦点时提交
  close() {
    this.edit = false;
    if(this.tag === ''){
      this.tag='暂无'
    }
    this.$emit('input', this.tag)
  }
}
}
</script>

<style lang="less" scoped>
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
